package com.xingqing.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xingqing.domain.dto.EmpDTO;
import com.xingqing.domain.entity.Emp;
import com.xingqing.domain.entity.WorkExperience;
import com.xingqing.domain.vo.EmpVO;
import com.xingqing.domain.vo.PageBean;
import com.xingqing.repository.EmpMapper;
import com.xingqing.service.EmpService;
import com.xingqing.service.WorkExperienceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;
    
    @Autowired
    private WorkExperienceService workExperienceService;

    @Override
    public PageBean<EmpVO> pageWithDept(Integer page, Integer pageSize) {
        // 开启分页查询
        PageHelper.startPage(page, pageSize);
        
        // 执行查询
        List<EmpVO> empList = empMapper.selectAllWithDept();
        
        // 获取分页信息
        Page<EmpVO> p = (Page<EmpVO>) empList;
        
        // 封装PageBean对象
        return new PageBean<>(p.getTotal(), p.getResult());
    }

    @Override
    public List<EmpVO> listWithDept() {
        return empMapper.selectAllWithDept();
    }

    @Override
    public EmpVO getByIdWithDept(Long id) {
        EmpVO emp = empMapper.selectByIdWithDept(id);
        if (emp == null) {
            throw new com.xingqing.exception.NotFoundException("员工", id);
        }
        return emp;
    }

    @Override
    public List<EmpVO> getByDeptIdWithDept(Long deptId) {
        return empMapper.selectByDeptIdWithDept(deptId);
    }

    @Override
    public List<Emp> list() {
        return empMapper.selectAll();
    }

    @Override
    public Emp getById(Long id) {
        return empMapper.selectById(id);
    }

    @Override
    public List<Emp> getByDeptId(Long deptId) {
        return empMapper.selectByDeptId(deptId);
    }

    @Override
    public void save(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.insert(emp);
    }

    @Override
    @Transactional
    public void saveWithWorkExperiences(EmpDTO empDTO) {
        // 1. 保存员工基本信息
        Emp emp = new Emp();
        BeanUtils.copyProperties(empDTO, emp);
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.insert(emp);
        
        log.info("员工保存成功，ID: {}", emp.getId());
        
        // 2. 保存工作经历
        List<WorkExperience> workExperiences = empDTO.getWorkExperiences();
        if (workExperiences != null && !workExperiences.isEmpty()) {
            // 设置员工ID
            for (WorkExperience workExperience : workExperiences) {
                workExperience.setEmpId(emp.getId());
            }
            workExperienceService.saveBatch(workExperiences);
            log.info("保存{}条工作经历", workExperiences.size());
        }
    }

    @Override
    public void updateById(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateById(emp);
    }

    @Override
    public void deleteById(Long id) {
        empMapper.deleteById(id);
    }

    @Override
    public void deleteByDeptId(Long deptId) {
        log.info("删除部门下所有员工，部门ID: {}", deptId);
        empMapper.deleteByDeptId(deptId);
    }
}